
import {
  getRepReportParamGroupListApi,
  addRepReportParamGroupApi,
  modifyRepReportParamGroupApi,
  getNodeListBySmi,
  removeRepReportParamGroupApi
} from '@/services/packetPush.js';
import { message } from 'ant-design-vue';

const deepFormat = (list) => {
  const geterList = [...list];
  geterList.forEach(item => {
    item.key = Math.random().toString(32);
    if (item.childrenType) {
      item.childrenType = deepFormat(item.childrenType);
    }
  });
  return geterList;
};
export default {
  namespaced: true,
  state: {
    addReportParamGroupListLoading: false,
    repReportParamGroupListLoading: false,
    repNodeParamGroupListLoading: false,
    repReportParamGroupList: [],
    repNodeParamGroupList: [],
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  getters: {
    nodeList(state) {
      return deepFormat(state.repNodeParamGroupList);
    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 新增
    async addRepReportParamGroup({ commit }, params) {
      commit('setState', { key: 'addReportParamGroupListLoading', payload: true });
      try {
        const rep = await addRepReportParamGroupApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addReportParamGroupListLoading', payload: false });
      }
    },
    // 修改
    async modifyRepReportParamLabel({ commit }, params) {
      commit('setState', { key: 'addReportParamGroupListLoading', payload: true });
      try {
        const rep = await modifyRepReportParamGroupApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addReportParamGroupListLoading', payload: false });
      }
    },
    // 删除
    async removeRepReportParamGroup({ commit }, params) {
      try {
        const rep = await removeRepReportParamGroupApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    },
    // 获取列表
    async getRepReportParamGroupList({ state, commit }, params) {
      commit('setState', { key: 'repReportParamGroupListLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum: pageNum,
        pageSize,
        ...params
      };

      const rep = await getRepReportParamGroupListApi(apiParams);
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'repReportParamGroupList', payload: rep.data.list });
      commit('setState', { key: 'repReportParamGroupListLoading', payload: false });
    },
    // 获取NODE列表
    async getRepNodeParamGroupList({ commit }, params) {
      commit('setState', { key: 'repNodeParamGroupListLoading', payload: true });
      const rep = await getNodeListBySmi(params);
      commit('setState', { key: 'repNodeParamGroupList', payload: rep.data });
      commit('setState', { key: 'repNodeParamGroupListLoading', payload: false });
    }
  }
};
